package com.matrix.rabbitmqtest.topic;

import cn.hutool.core.lang.Console;
import com.matrix.rabbitmqtest.util.AppKeys;
import com.matrix.rabbitmqtest.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 第二个Consumer
 */
public class Consumer2 {

    public static final String queueName = "Q2";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtil.getChannel();
        channel.exchangeDeclare(AppKeys.TOPIC_EXCHANGE, BuiltinExchangeType.TOPIC);
        channel.queueDeclare(queueName, false, false, true, null);
        channel.queueBind(queueName, AppKeys.TOPIC_EXCHANGE, "*.orange.*"); // 接收指定主题的消息
        Console.log("{} 接收消息...", queueName);
        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                Console.log("{} Receiving {} : {}", queueName, envelope.getDeliveryTag(), new String(body, StandardCharsets.UTF_8));
            }
        });
    }
}
